package com.hitnet.hitnet.pic;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hitnet.hitnet.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by xuanli.
 */
public class MyPageAdapter extends BasePagerAdapter
{
	private List<String> selectedImgsPath;// the path of selected images
	private String imgPath;//
	private Context context;
	private LayoutInflater inflater;

	public MyPageAdapter(Context context, List<String> lt)
	{
		super(lt.size());
		this.context = context;
		this.selectedImgsPath = lt == null ? new ArrayList<String>() : lt;
		inflater = LayoutInflater.from(context);
	}
	
	public void setPrimaryItem(ViewGroup container, int position,
			Object object) {
		super.setPrimaryItem(container, position, object);
		((BaseViewPager)container).mCurrentView = (DragImageView)object;
	}

	public void setDatas(List<String> datas)
	{
		selectedImgsPath = datas;
	}

	@Override
	public int getCount()
	{
		return selectedImgsPath == null ? 1 : selectedImgsPath.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object)
	{
		return view == object;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position)
	{
		Bitmap bitmap = null;
		try{
			BitmapFactory.Options opt = new BitmapFactory.Options();
	        opt.inPreferredConfig = Bitmap.Config.RGB_565;
	        opt.inPurgeable = true;
	        opt.inInputShareable = true;
	        opt.inSampleSize = 2;
			bitmap = BitmapFactory.decodeFile(selectedImgsPath.get(position), opt);
		}catch(OutOfMemoryError e){
			BitmapFactory.Options opt = new BitmapFactory.Options();
	        opt.inPreferredConfig = Bitmap.Config.RGB_565;
	        opt.inPurgeable = true;
	        opt.inInputShareable = true;
	        opt.inSampleSize = 4;
			bitmap = BitmapFactory.decodeFile(selectedImgsPath.get(position), opt);
		}
		if (bitmap == null)
		{
			try{
				BitmapFactory.Options opt = new BitmapFactory.Options();
		        opt.inPreferredConfig = Bitmap.Config.RGB_565;
		        opt.inPurgeable = true;
		        opt.inInputShareable = true;
				bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.friends_sends_pictures_no, opt);
			}catch(OutOfMemoryError e){
				e.printStackTrace();
			}
		}
		View view = inflater.inflate(R.layout.zoom_image_layout, null);
		DragImageView dragImageView = (DragImageView) view.findViewById(R.id.zoom_image_view);
		dragImageView.setImageBitmap(bitmap);
		container.addView(view);
		return view;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object)
	{
		View view = (View) object;
		DragImageView div = (DragImageView)object;
		Drawable drawable = div.getDrawable();
		if(drawable != null){
			if (drawable instanceof BitmapDrawable) {
				BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
				Bitmap bitmap = bitmapDrawable.getBitmap();
				if (bitmap != null)
					bitmap.recycle();
			}
		}
		container.removeView(view);
	}
}
